package com.training.killerapp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.plexus.util.StringUtils;

import com.training.killerapp.model.Page;
import com.training.killerapp.model.User;

public class ManageAccountServlet extends AbstractKillerAppServlet
{
    protected void doPost( HttpServletRequest request, HttpServletResponse response ) 
        throws ServletException, IOException
    {
        if( StringUtils.isNotEmpty( request.getParameter( "friendsubmit" ) ) )
        {
            addFriends( request, response );
        }
        else if( StringUtils.isNotEmpty( request.getParameter( "contentsubmit" ) ) )
        {
            updateContents( request, response );
        }
        else
        {
            createAccount( request, response );
        }
    }

    private void updateContents( HttpServletRequest request, HttpServletResponse response )
        throws ServletException, IOException
    {
        String contents = request.getParameter( "contents" );
        String pageName = request.getParameter( "pageName" );

        User currentUser = getCurrentUser( request );

        KillerApp killerApp = retrieveKillerApp();
        try
        {
            killerApp.managePage( currentUser, pageName, contents );
        }
        finally
        {
            releaseKillerApp( killerApp );
        }

        response.sendRedirect( "show?email=" + currentUser.getEmail() );
    }

    private void addFriends( HttpServletRequest request, HttpServletResponse response )
        throws ServletException, IOException
    {
        String[] friends = request.getParameterValues( "friend" );
        if( friends != null )
        {
            User currentUser = getCurrentUser( request );
            if( currentUser == null )
            {
                response.sendRedirect( "index.jsp?message=Must Be Logged in to Add Friends" );
            }

            KillerApp killerApp = retrieveKillerApp();
            try
            {
                for ( int i = 0; i < friends.length; i++ )
                {
                    killerApp.addFriend( currentUser, friends[i] );
                }
            }
            finally
            {
                releaseKillerApp( killerApp );
            }

            response.sendRedirect( "show?email=" + currentUser.getEmail() );
        }
    }

    private void createAccount( HttpServletRequest request, HttpServletResponse response )
        throws ServletException, IOException
    {
        String email = request.getParameter( "email" );
        String password = request.getParameter( "password" );
        String name = request.getParameter( "name" );

        response.setContentType("text/html");

        KillerApp killerApp = retrieveKillerApp();
        try
        {
            User newUser = killerApp.createAccount( email, password, name );

            setCurrentUser( request, newUser );

            response.sendRedirect( "list" );
        }
        finally
        {
            releaseKillerApp( killerApp );
        }
    }
}
